package com.example.springadvance.a01;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.util.Arrays;
import java.util.Locale;

@SpringBootApplication
public class Application {

    public static void main(String[] args) throws IOException {
        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);


        // ResourceLoader
        System.out.println(context.getResource("classpath:application.properties"));
        Resource[] rs = context.getResources("classpath*:/META-INF/spring.factories");
        for (Resource r: rs) {
            System.out.println(r.getFilename());
        }


        // MessageSource
        String zhMsg = context.getMessage("hi", null, Locale.CHINA);
        String enMsg = context.getMessage("hi", null, Locale.ENGLISH);
        System.out.println("zhMsg = " + zhMsg);
        System.out.println("enMsg = " + enMsg);

        // EnvironmentCapable
        ConfigurableEnvironment env = context.getEnvironment();
        MutablePropertySources sources = env.getPropertySources();
        System.out.println("配置源 size: " + sources.size());
        sources.stream()
                        .forEach(s -> System.out.println(s.getName()));

        // ApplicationEventPublisher
        if (context instanceof ApplicationEventPublisher publisher) {
            publisher.publishEvent(new ApplicationEvent(context) {
                @Override
                public Object getSource() {
                    return context;
                }
            });
        }

        context.close();

    }
}
